﻿using Games.Common;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Games.FlipBlocks
{
    class Cell : Image
    {
        private static readonly ImageSource ImgFocus = ImageSource.FromFile("flip_focus.png");
        private static readonly Dictionary<int, ImageSource> ImgBombs = new Dictionary<int, ImageSource> {
            { 1,ImageSource.FromFile("flip_bomb_1.png")},
            { 2,ImageSource.FromFile("flip_bomb_2.png")},
            { 3,ImageSource.FromFile("flip_bomb_3.png")},
            { 4,ImageSource.FromFile("flip_bomb_4.png")},
            { 5,ImageSource.FromFile("flip_bomb_5.png")},
        };
        internal static Color[] Colors = new Color[] {
            Color.Black,
            Color.Red,
            Color.LimeGreen,
            Color.Blue,
            Color.Fuchsia,
            Color.Gray,
            Color.Coral,
            Color.Olive,
            Color.Orange,
            Color.Teal,
            Color.MidnightBlue,
            Color.SaddleBrown,
            Color.DarkSlateGray,
            Color.DarkGreen,
        };
        public int ID { get; private set; }

        public void Set(int id)
        {
            this.ID = id;
            this.BackgroundColor = Colors[id];
        }
        public async Task SetTo(int id, uint len)
        {
            var ccolor = this.BackgroundColor;
            var color = Colors[id];
            await this.ColorTo(ccolor, color, x => this.BackgroundColor = x, len);
            this.ID = id;
        }
        public void Bomb(int i)
        {
            if(i > 0 && i <= 5)
            {
                this.Source = ImgBombs[i];
            }
            else
            {
                this.Source = null;
                this.Scale = 1;
            }
        }
        public void Focus(bool focus = true)
        {
            if(focus)
            {
                this.Source = ImgFocus;
            }
            else
            {
                this.Source = null;
            }
        }

        public void ClearFast()
        {
            this.BackgroundColor = Color.Transparent;
            this.Source = null;
        }
        public async Task Clear(uint len)
        {
            await this.FadeTo(0, len);
            this.BackgroundColor = Color.Transparent;
            this.Opacity = 1;
        }
    }
}
